package online.westonline.com.westonline.ui;

import android.graphics.Color;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.TextView;

import com.grandbroad.online.greendao.NodeBeanDao;


import org.greenrobot.greendao.query.QueryBuilder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import online.westonline.com.westonline.R;
import online.westonline.com.westonline.adapter.NodeInfoAdpter;
import online.westonline.com.westonline.base.BaseActivity;
import online.westonline.com.westonline.entry.NodeBean;
import online.westonline.com.westonline.utils.GreenDaoUtils;
import online.westonline.com.westonline.utils.UiUtlis;

public class NodeInfoActivity extends BaseActivity {


    @BindView(R.id.tv_topbar_title)
    TextView tvTopbarTitle;
    @BindView(R.id.tv_topbar_title_center)
    TextView tvTopbarTitleCenter;
    @BindView(R.id.img_toolbar_menu)
    ImageView imgToolbarMenu;
    @BindView(R.id.tv_topbar_menu)
    TextView tvTopbarMenu;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.recyclerview_nodeinfo)
    RecyclerView recyclerviewNodeinfo;
    private String type;
    private int start_pos;
    private NodeBeanDao beanDao;
    private List<NodeBean> nodeBeans = new ArrayList<>();
    private NodeInfoAdpter nodeInfoAdpter;
    @Override
    protected void initview() {
        toolbar.setBackgroundColor(UiUtlis.getColor(R.color.colorAppTheme));
        toolbar.setTitleTextColor(UiUtlis.getColor(R.color.colorMenuBtn));
        toolbar.setTitle(type);
        tvTopbarTitleCenter.setText("详情");
        tvTopbarTitleCenter.setTextColor(Color.BLACK);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowTitleEnabled(true);
        initrecyclerview();
    }

    private void initrecyclerview() {
        nodeInfoAdpter = new NodeInfoAdpter();
        recyclerviewNodeinfo.setLayoutManager(new LinearLayoutManager(this));
        recyclerviewNodeinfo.setAdapter(nodeInfoAdpter);
        nodeInfoAdpter.setDatas(nodeBeans);
        nodeInfoAdpter.notifyDataSetChanged();
        //滚动到选择的位置
        if (start_pos!= -1) {
            recyclerviewNodeinfo.scrollToPosition(start_pos);
            LinearLayoutManager mLayoutManager =
                    (LinearLayoutManager) recyclerviewNodeinfo.getLayoutManager();
            mLayoutManager.scrollToPositionWithOffset(start_pos, 0);
        }
    }

    @Override
    protected void initdata() {
        //獲取查詢條件
        type = getIntent().getExtras().getString("type");
        start_pos = getIntent().getExtras().getInt("pos");
        //獲取數據庫實體
        beanDao = GreenDaoUtils.getInstance().getDaoSession().getNodeBeanDao();
        //查询
        if (!TextUtils.isEmpty(type))
        {
            QueryBuilder<NodeBean> beanQueryBuilder = beanDao.queryBuilder();
            //查询数据
            nodeBeans = beanQueryBuilder.where(NodeBeanDao.Properties.Type.eq(type)).build().list();
        }
    }

    @Override
    protected int setlayout() {
        return R.layout.activity_node_info;
    }
}
